package com.aia.ilp.web.sanity;

import org.apache.log4j.Logger;

import java.util.List;


import com.aia.ilp.service.MainService;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;

public class TestsmsAction extends ActionSupport {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TestsmsAction.class);

	private static final long serialVersionUID = 1L;
	private MainService mainService;
	private String mobile;
//	public void validate() {
//		if (mobile == null || mobile.length() != 11) {
//			addFieldError("mobile", "请输入正确的手机号");
//		}
//
//	}

	/***
	 * nouse
	 */
	public String execute() {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		addActionMessage("手机号:" + mobile);
//		List activedCusts = mainService.getActivedCustsByMobile(mobile);
//		int activedCustsCount = 0;
//		if (activedCusts != null && activedCusts.size() > 0) {
//			activedCustsCount = activedCusts.size();
//		}
//		addActionMessage("查询到注册该手机号并短信激活的用户数量:" + activedCustsCount);
//
//		List allCusts = mainService.getAllCustsByMobile(mobile);
//		int allCustsCount = 0;
//		if (allCusts != null && allCusts.size() > 0) {
//			allCustsCount = allCusts.size();
//		}
//		addActionMessage("查询到注册该手机号的所有的用户数量:" + allCustsCount);

		List list = mainService.getSmslogToday(mobile);
		int countSent = 0;
		if (list == null || list.size() == 0) {
			countSent = 0;
		} else {
			countSent = list.size();
		}
		addActionMessage("查询短信记录该手机号今日已经发送短信条数:" + countSent);
		String mail = "sanitytest@aia.com";
//		String custGUID = "SANITY-XXX-XXX";

		String verifyCode = Tools.randSixNumberString();
		addActionMessage("生成验证码:" + verifyCode + ", 短信内容:\""+smsContent(verifyCode)+"\"");
		String cityCode = ""; // 保留
		/*
		 * 把验证码存在cust表中以备验证 mainService.updateverifyCodeForCust(custGUID,
		 * verifyCode);
		 */
		mainService.sendSMS(mobile, cityCode, verifyCode, mail);

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
	
	private String smsContent(String argCode) {
		String s = "";
		if (argCode == null) {
			s = "";
		} else {
			s = argCode;
		}
		return "感谢您的参与，您的手机验证密码为" + s + "";
	}

	public void setMainService(MainService mainService) {
		this.mainService = mainService;
	}

	public MainService getMainService() {
		return mainService;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getMobile() {
		return mobile;
	}

}
